package com.capricornus.demo.service.user;

import com.capricornus.demo.common.ApplicationException;
import com.capricornus.demo.dao.DepartmentMapper;
import com.capricornus.demo.model.Department;
import com.capricornus.demo.model.DepartmentTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    public Department selectDeptInfo(String deptId) {
        return departmentMapper.selectDeptInfo(deptId);
    }

    @Override
    public List<Department> allDeptList() {
        return departmentMapper.allDeptList();
    }

    @Override
    public List<DepartmentTree> selectDepartmentTreeList() {
        List<Department> allDeptList = departmentMapper.allDeptList();
        Map<String, DepartmentTree> departmentTreeMap = new LinkedHashMap<>();
        for (Department dbDepartment : allDeptList) {
            DepartmentTree departmentTree = new DepartmentTree();
            departmentTree.setDeptId(dbDepartment.getDeptId());
            departmentTree.setDeptCode(dbDepartment.getDeptCode());
            departmentTree.setDeptName(dbDepartment.getDeptName());
            departmentTree.setParentId(dbDepartment.getParentId());
            departmentTree.setValue(dbDepartment.getDeptCode());
            departmentTree.setLabel(dbDepartment.getDeptName());
            departmentTree.setDeptMark(dbDepartment.getDeptMark());
            departmentTree.setChildren(new ArrayList<>());
            departmentTreeMap.put(departmentTree.getDeptCode(), departmentTree);
        }
        List<DepartmentTree> departmentTreeList = new ArrayList<>();
        //构建部门树型
        for (Map.Entry<String, DepartmentTree> entries : departmentTreeMap.entrySet()) {
            String keys = entries.getKey();
            DepartmentTree currentDepartmentTree = entries.getValue();
            //获取上级部门字段ParentId
            DepartmentTree parentDepartmentTree = departmentTreeMap.get(currentDepartmentTree.getParentId());
            if (parentDepartmentTree != null) {
                //上级部门字段ParentId不为空，然后获得上级部门字段给了children
                List<DepartmentTree> children = parentDepartmentTree.getChildren();
                if (children == null) {
                    children = new ArrayList<>();
                }
                //获取到下级部门字段ParentId對象添加進來
                children.add(currentDepartmentTree);
            } else {
                //字段ParentId为空就直接进这里，没获取到顶级对象就走上面判断
                departmentTreeList.add(currentDepartmentTree);
            }
        }
        return departmentTreeList;
    }

    @Override
    public Department getDeptName(String parentId) {

        return departmentMapper.getDeptName(parentId);
    }

    @Override
    public int insert(Department department) {
        int countInsert = departmentMapper.insert(department);
        if (countInsert <= 0) {
            throw new ApplicationException("新增部门失败");
        }
        return countInsert;
    }

    @Override
    public int update(Department department) {
        int countUpdate = departmentMapper.update(department);
        if (countUpdate <= 0) {
            throw new ApplicationException("编辑部门失败");
        }
        return countUpdate;
    }


    @Override
    public int deleteDepartmentId(Department department) {
        //获取到当前需要删除的部门编码
        String deptCode = department.getDeptCode();
        //用部门编码查询获取该部门所有的子级部门信息
       int departmentCount = departmentMapper.getDeptNameList(deptCode);
        if (departmentCount > 0){
            throw new ApplicationException("该部门下存在子级部门！");
        }
        int deleteCount = departmentMapper.deleteDepartmentId(department.getDeptId());
        if (deleteCount < 1) {
            throw new ApplicationException("删除部门失败");
        }
        return deleteCount;
    }

    @Override
    public int selectDepartmentId(String deptCode) {
        int count = departmentMapper.selectDepartmentId(deptCode);
        return count;
    }

//    @Override
//    public boolean selectDepartmentExist(String deptCode) {
//
//        return departmentMapper.selectDepartmentExist(deptCode);
//    }
}
